%load('SVM Gijs/LD/SVM Results/ROIs 17-Jun-2010.mat');
%load('SVM Gijs/MJ/SVM Results/ROIs 17-Jun-2010.mat');
%load('SVM Gijs/MJ/SVM Results/ROIs 22-Jun-2010 shuffled.mat');

%%%
%load('C:/Users/Yoren/Desktop/SVM/SVM Gijs/TJ/SVM Results/ROIs 22-Jun-2010.mat');


%data.StateTrainingQualification;
%data.WantedTrainingQualification;
%ROI=1;
%SVMpass=2;

disp '==================='

subjects = {'LD','TA','MJ','MM','TJFCD','TK','TJ'};
%subjects = {'TJFCD'};
subjects = {'MJ'};

saveresults=[];

%svmpenalties = 'abspredTimecourse';
%svmpenalties = 'predTimecourse';
svmpenalties = 'testprediction';

savefolder = ['mcfig-' svmpenalties];

[g gg ggg]=mkdir(savefolder);

LLcol=[0 1 0];
RLcol=[1 0 0];
LRcol=[0 1 1];
RRcol=[1 0 1];

for SUB=subjects
    
    for SHUF=[0 1]
        
        clear ('results','data','goodpred','Testrunlogical','fivecolortimecourse','cfg');
        if SHUF
            load(['C:/Users/Yoren/Desktop/SVM/SVM Gijs/' SUB{1} '/SVM Results/ROIs 05-Jul-2010 shuffled.mat']);
            disp('shuffled');
        else
            load(['C:/Users/Yoren/Desktop/SVM/SVM Gijs/' SUB{1} '/SVM Results/ROIs 05-Jul-2010.mat']);
            disp('non-shuffled');
        end
    
        
        %for ROI=1:size(results,2); %dit werkt neit omdat er nooude roi
        %data achterblijft.. bug is inmiddels gefixt, maar geen zin om
        %svmrun nogmaals te draaien.
        for ROI=1:size(cfg.ROIs,2)
            disp (['ROI ' num2str(ROI) ': ' results{ROI}.ROI]);

            goodpred = [];

            for SVMpass = 1:cfg.SVMruns %1:size(results{ROI}.LLvsLRTimecourse.TestrunsIndex{1},2)

                %testrunlogical bevat 1en voor de trainingruns and 0en voor de testruns.
                %disp  (cfg.SVMruns);
                Testrunlogical       = ones(size(data.LLvsLRTimecourse));
                Testrunlogical(:,results{ROI}.LLvsLRTimecourse.TestrunsIndex{SVMpass}) = 0;

                %tst = results{ROI}.LLvsLRTimecourse.actualuseTest{SVMpass};
                %tst2 = results{ROI}.LLvsRRTimecourse.actualuseTest{SVMpass};
                
                %figure (3);
                %subplot(2,1,1);
                %imagesc(tst);
                %subplot(2,1,2);
                %imagesc(Testrunlogical);
                %colormap([[1 1 1];[0 0 0]]);
                
                %StateTrainingQualification = data.StateTrainingQualification  (Testrunlogical & results{ROI}.LLvsLRTimecourse.actualuseTest{SVMpass});
                %WantedTrainingQualification = data.WantedTrainingQualification(Testrunlogical & results{ROI}.LLvsLRTimecourse.actualuseTest{SVMpass});

                fivecolortimecourse = zeros(size(data.LLselection(Testrunlogical & results{ROI}.LLvsLRTimecourse.actualuseTest{SVMpass})));

                %L=CW, R=CCW. LR->wanted left, state right
                %1= links, -1=rechts
                % LL=1 LR=2 RL=3 RR=4
                
                fivecolortimecourse = fivecolortimecourse + 1*data.LLselection(Testrunlogical & results{ROI}.LLvsLRTimecourse.actualuseTest{SVMpass});
                fivecolortimecourse = fivecolortimecourse + 2*data.LRselection(Testrunlogical & results{ROI}.LLvsLRTimecourse.actualuseTest{SVMpass});
                fivecolortimecourse = fivecolortimecourse + 3*data.RLselection(Testrunlogical & results{ROI}.LLvsLRTimecourse.actualuseTest{SVMpass});
                fivecolortimecourse = fivecolortimecourse + 4*data.RRselection(Testrunlogical & results{ROI}.LLvsLRTimecourse.actualuseTest{SVMpass});
                
                
                orgfivecolortimecourse = zeros(size(data.LLselection(Testrunlogical & results{ROI}.LLvsLRTimecourse.actualuseTest{SVMpass})));
                
                orgfivecolortimecourse(data.orgWantedTimecourse==-cfg.CCWcode & data.orgStateTimecourse==-cfg.CCWcode) = 1;
                orgfivecolortimecourse(data.orgWantedTimecourse==-cfg.CCWcode & data.orgStateTimecourse== cfg.CCWcode) = 2;
                orgfivecolortimecourse(data.orgWantedTimecourse== cfg.CCWcode & data.orgStateTimecourse==-cfg.CCWcode) = 3;
                orgfivecolortimecourse(data.orgWantedTimecourse== cfg.CCWcode & data.orgStateTimecourse== cfg.CCWcode) = 4;


                switch svmpenalties
                    case 'predTimecourse'

                        LLTimecourse =   results{ROI}.LLvsLRTimecourse.predTimecourse{SVMpass} ...
                                       + results{ROI}.LLvsRLTimecourse.predTimecourse{SVMpass} ...
                                       + results{ROI}.LLvsRRTimecourse.predTimecourse{SVMpass} ;

                        LRTimecourse = - results{ROI}.LLvsLRTimecourse.predTimecourse{SVMpass} ...
                                       + results{ROI}.LRvsRLTimecourse.predTimecourse{SVMpass} ...
                                       + results{ROI}.LRvsRRTimecourse.predTimecourse{SVMpass} ;

                        RLTimecourse =   results{ROI}.RLvsRRTimecourse.predTimecourse{SVMpass} ...
                                       - results{ROI}.LLvsRLTimecourse.predTimecourse{SVMpass} ...
                                       - results{ROI}.LRvsRLTimecourse.predTimecourse{SVMpass} ;

                        RRTimecourse = - results{ROI}.RLvsRRTimecourse.predTimecourse{SVMpass} ...
                                       - results{ROI}.LRvsRRTimecourse.predTimecourse{SVMpass} ...
                                       - results{ROI}.LLvsRRTimecourse.predTimecourse{SVMpass} ;

                    case 'abspredTimecourse'

                        LLTimecourse = max(0,  results{ROI}.LLvsLRTimecourse.predTimecourse{SVMpass}) + ...
                                       max(0,  results{ROI}.LLvsRLTimecourse.predTimecourse{SVMpass}) + ...
                                       max(0,  results{ROI}.LLvsRRTimecourse.predTimecourse{SVMpass}) ;

                        LRTimecourse = max(0,- results{ROI}.LLvsLRTimecourse.predTimecourse{SVMpass}) + ...
                                       max(0,  results{ROI}.LRvsRLTimecourse.predTimecourse{SVMpass}) + ...
                                       max(0,  results{ROI}.LRvsRRTimecourse.predTimecourse{SVMpass}) ;

                        RLTimecourse = max(0,  results{ROI}.RLvsRRTimecourse.predTimecourse{SVMpass}) + ...
                                       max(0,- results{ROI}.LLvsRLTimecourse.predTimecourse{SVMpass}) + ...
                                       max(0,- results{ROI}.LRvsRLTimecourse.predTimecourse{SVMpass}) ;

                        RRTimecourse = max(0,- results{ROI}.RLvsRRTimecourse.predTimecourse{SVMpass}) + ...
                                       max(0,- results{ROI}.LRvsRRTimecourse.predTimecourse{SVMpass}) + ...
                                       max(0,- results{ROI}.LLvsRRTimecourse.predTimecourse{SVMpass}) ;

                    case 'testprediction'

                        LLTimecourse =   results{ROI}.LLvsLRTimecourse.testprediction{SVMpass} ...
                                       + results{ROI}.LLvsRLTimecourse.testprediction{SVMpass} ...
                                       + results{ROI}.LLvsRRTimecourse.testprediction{SVMpass} ;

                        LRTimecourse = - results{ROI}.LLvsLRTimecourse.testprediction{SVMpass} ...
                                       + results{ROI}.LRvsRLTimecourse.testprediction{SVMpass} ...
                                       + results{ROI}.LRvsRRTimecourse.testprediction{SVMpass} ;

                        RLTimecourse =   results{ROI}.RLvsRRTimecourse.testprediction{SVMpass} ...
                                       - results{ROI}.LLvsRLTimecourse.testprediction{SVMpass} ...
                                       - results{ROI}.LRvsRLTimecourse.testprediction{SVMpass} ;

                        RRTimecourse = - results{ROI}.RLvsRRTimecourse.testprediction{SVMpass} ...
                                       - results{ROI}.LRvsRRTimecourse.testprediction{SVMpass} ...
                                       - results{ROI}.LLvsRRTimecourse.testprediction{SVMpass} ;

                end

                [b,c]=max([LLTimecourse LRTimecourse RLTimecourse RRTimecourse],[],2) ;        
                %disp(c');

                frombuttonpress=fivecolortimecourse(:,1)';
                orgfrombuttonpress=orgfivecolortimecourse(Testrunlogical & results{ROI}.LLvsLRTimecourse.actualuseTest{SVMpass},1)';
                
                
                %disp(size(frombuttonpress));
                %disp(size(c,1));
                %disp (size(results{1,1}.LLvsLRTimecourse.predTimecourse{1}));
                %size(data.StateTrainingQualification)

                %fromsvm = zeros(size(frombuttonpress));
                %fromsvm(:,end-size(c,1)+1:end) = c';

                fromsvm = c';

                figure(1);
                subplot(2,1,1);
                colormap([[1 1 1];LLcol;LRcol;RLcol;RRcol]);
                imagesc([orgfrombuttonpress; frombuttonpress; fromsvm; (frombuttonpress==fromsvm)*1]);

                subplot(2,1,2);
                set(gcf,'DefaultAxesColorOrder',[LLcol;LRcol;RLcol;RRcol]);
                plot([LLTimecourse LRTimecourse RLTimecourse RRTimecourse]);
                legend('LL','LR','RL','RR','Location','EastOutside');
                
                goodpredicted=sum(frombuttonpress==fromsvm);
                totalpredicted=sum(frombuttonpress~=0,2);
                
                title(['Subject: ' SUB{1} ', ROI: ' results{ROI}.ROI ', SVMpass=' num2str(SVMpass) ',shuffled=' num2str(SHUF) ', good predictons:' num2str(goodpredicted) '/' num2str(totalpredicted) ' = ' num2str(goodpredicted/totalpredicted)]);

                disp(['saving plot: ' savefolder '/' SUB{1} '-ROI ' results{ROI}.ROI '-pass ' num2str(SVMpass) '-shuf=' num2str(SHUF) '.png']);
                saveas(1,[savefolder '/' SUB{1} '-ROI ' results{ROI}.ROI '-pass ' num2str(SVMpass) '-shuf=' num2str(SHUF) '.png']);
              
                %disp(['good predictions: ' num2str(goodpredicted) ' / ' num2str(totalpredicted) ' = ' num2str(goodpredicted/totalpredicted)]);
                goodpred(end+1) = goodpredicted/totalpredicted;
            end

            disp(['avg prediction: ' num2str(mean(goodpred)) ' +/- ' num2str(std(goodpred))]);
            
            if SHUF
                saveresults.shuf.mean.(SUB{1})(ROI) = mean(goodpred);
                saveresults.shuf.std.(SUB{1})(ROI)  = std(goodpred);
                saveresults.shuf.ROIs.(SUB{1}){ROI} = results{ROI}.ROI;
            else
                saveresults.nonshuf.mean.(SUB{1})(ROI) = mean(goodpred);
                saveresults.nonshuf.std.(SUB{1})(ROI)  = std(goodpred);
                saveresults.nonshuf.ROIs.(SUB{1}){ROI} = results{ROI}.ROI; %zinloos?
            end
        end
        
    end
end

figure(2);
for i=1:size(subjects,2)
    subplot(size(subjects,2),1,i);
    errorbar([saveresults.shuf.mean.(subjects{i});saveresults.nonshuf.mean.(subjects{i})]',[saveresults.shuf.std.(subjects{i});saveresults.nonshuf.std.(subjects{i})]')
      
    set(gca,'XTick',1:size(saveresults.shuf.ROIs.(subjects{i}),2));
    set(gca,'XTickLabel',saveresults.shuf.ROIs.(subjects{i}));

    title(['subject' subjects{i}]);
    
    legend('Shuffled','Non-shuffled','Location','EastOutside');
end

disp(['saving plot: ' savefolder '/endresult.png']);
saveas(2,[savefolder '/endresult.png']);

disp(saveresults);